<script setup lang="ts">
import applicationApi from '@/api/application'
import CollectForm from '@/views/agent/chat/components/CollectForm.vue'
import OpeningRemarks from '@/views/agent/chat/components/OpeningRemarks.vue'
import StepLoading from '@/views/agent/chat/components/StepLoading.vue'
import UploadFile from '@/views/agent/chat/components/UploadFile.vue'
import { SpeechPreparationProcessLoading, speechPreparationAgent } from './data'
import { computed, ref } from 'vue'

const tip = 'AI风格发言稿'
const formConfig = [
  {
    title: '输入背景信息',
    slotName: 'one'
  },
  {
    title: '发言稿需要包含的关键点',
    slotName: 'two',
    description: '请输入发言稿需要包含的关键点，及撰写要求'
  },
  {
    title: '上传历史发言稿',
    slotName: 'three'
  },
]

const props = withDefaults(
  defineProps<{
    appId?: any
    chatId?: string
    applicationDetails: any
  }>(),
  {
  }
)
const step = ref(0)
const loading = ref(false)
const topTips = ref(tip)
const resultContent = ref('')

const formData: any = ref({
  backInfo: '',
  keyPoints: '',
  file: [],
})

function onBack () {
  step.value = 0;
}
const setDocumentList = (val: any) => {
  formData.value.file = val
}

const hasSubmit = computed(() => {
  return formData.value.backInfo && formData.value.keyPoints && !loading.value
})

async function onSubmit () {
  try {
    step.value = 1;
    loading.value = true
    const chatData = await applicationApi.getChatOtherOpen(speechPreparationAgent.appId, speechPreparationAgent.apiKey)
    const message = `背景信息:${formData.value.backInfo};关键点:${formData.value.keyPoints};`
    console.log(message)
    const data = await applicationApi.postChatMessageNew(chatData.data, { document_list:formData.value.file, stream: false, message: message, apiKey: speechPreparationAgent.apiKey })
    resultContent.value = data.data?.content || '--'
  } catch (e) {
    console.log(e)
  } finally {
    loading.value = false;
  }
}
const active = computed(() => {
  if (formData.value.backInfo && !formData.value.keyPoints && (formData.value.file || []).length === 0) {
    return 1
  }
  if (formData.value.backInfo && formData.value.keyPoints &&  (formData.value.file || []).length === 0) {
    return 2
  }
  if (formData.value.backInfo && formData.value.keyPoints &&  (formData.value.file || []).length > 0) {
    return 3
  }
  return 0
})
</script>

<template>
  <OpeningRemarks class="mb-12" :title="props.applicationDetails.name" :description="props.applicationDetails.desc"  :avatar="props.applicationDetails.avatar" />
  <el-card class="mt-12 mb-12" shadow="never" v-if="step !== 1">
    <div class="ai-start">
      <div class="ai-start-tips">
        <el-icon @click="onBack" v-if="step === 1"><ArrowLeftBold /></el-icon>
        <div class="ai-start-tips-ellipsis">
          {{ topTips }}
        </div>
      </div>
      <el-button type="primary" size="small" @click="onSubmit" :disabled="!hasSubmit">开始分析</el-button>
    </div>
  </el-card>
  <CollectForm v-if="step === 0" :data="formConfig" :active="active">
    <template #one>
      <el-input type="textarea" rows="4" v-model="formData.backInfo" placeholder="请输入" />
    </template>
    <template #two>
      <el-input type="textarea" rows="5" v-model="formData.keyPoints" placeholder="请输入" />
    </template>
    <template #three>
      <UploadFile :app-id="speechPreparationAgent.appId" :api-key="speechPreparationAgent.apiKey" @update="setDocumentList" :max-files="10" name="文档" tip="支持格式：txt、pdf、doc、docx格式文件，<br />一次至多上传10个文档" accept=".txt,.pdf,.docx,.doc" />
    </template>
  </CollectForm>
  <StepLoading v-if="step === 1" :data="SpeechPreparationProcessLoading" :loading="loading" :result-content="resultContent" :applicationDetails="props.applicationDetails" />
</template>

<style scoped lang="scss">

</style>
